package jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

/*
封装数据库连接，单例模式
@author
 */
public class DbConn {
    private static Connection conn = null;
    private static Properties p = new Properties();

    /*
    静态代码块：累加载时被调用
    读取数据库配置文件db.properties
    加载驱动
     */
    static {
        try {
            InputStream inStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("db.properties");
            p.load(inStream);
            Class.forName(p.getProperty("driverClassName"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private DbConn(){

    }

    private static Connection createConn(){
        try{
            return DriverManager.getConnection(p.getProperty("url"),
                    p.getProperty("username"),p.getProperty("password"));
        }catch (Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,"数据库连接失败，请检查配置文件db.properties!");
        }
        return null;
    }

    /*
    打开数据库
    @return Connection 数据库连接对象
     */
    public static Connection openDB(){
        if(conn == null){
            conn = createConn();
        }
        return conn;
    }

    /*
    关闭数据库
     */
    public static void closeDB(){
        if(conn!=null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /*
    打开jdbc事务
     */
    public static void openShiwu(){
        if (conn!=null){
            try{
                conn.setAutoCommit(false);
            }catch (SQLException ex){
                Logger.getLogger(DbConn.class.getName()).log(Level.SEVERE,null,ex);
            }
        }
    }
    /*
    关闭jdbc事务
     */
    public static void closeShiwu(){
        if (conn!=null){
            try{
                conn.setAutoCommit(true);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    /*
    jdbc事务回滚
     */
    public static void rollback(){
        if (conn!=null){
            try{
                conn.rollback();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Connection conn = DbConn.openDB();
        if(conn != null){
            System.out.println("数据库连接成功");
        }
    }
}
